package com.littleu.yojoincn.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.littleu.yojoincn.common.exception.http.NotFoundException;
import com.littleu.yojoincn.dto.celebrity.AddressDTO;
import com.littleu.yojoincn.mapper.CelebrityAddressMapper;
import com.littleu.yojoincn.mapper.CelebrityMapper;
import com.littleu.yojoincn.model.CelebrityAddressDO;
import com.littleu.yojoincn.model.CelebrityDO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CelebrityService extends ServiceImpl<CelebrityMapper, CelebrityDO> {
    @Autowired
    private CelebrityAddressService celebrityAddressService;

    public List<CelebrityAddressDO> getCelebrityAddressList(Long celebrityId){
        return this.celebrityAddressService.getBaseMapper().selectAddressByCelebrityId(celebrityId);
    }

    public CelebrityAddressDO getCelebrityDefaultAddress(Long celebrityId){
        Integer isDefault = 1;
        return this.celebrityAddressService.getBaseMapper().selectDefaultAddressByCelebrityId(celebrityId, isDefault);
    }

    public void setCelebrityAddressDefault(Long cid, Long aid){
        this.celebrityAddressService.getBaseMapper().removeDefaultAddress(cid);
        CelebrityAddressDO exist = this.celebrityAddressService.getById(aid);
        if(exist == null){
            throw new NotFoundException(10002);
        }
        exist.setIsDefault(1);
        this.celebrityAddressService.updateById(exist);
    }

    public void createCelebrityAddress(AddressDTO dto){
        CelebrityAddressDO addressDO = new CelebrityAddressDO();
        BeanUtils.copyProperties(dto, addressDO);
        this.celebrityAddressService.save(addressDO);
    }

    public void updateCelebrityAddress(Long id, AddressDTO dto){
        CelebrityAddressDO exist = this.celebrityAddressService.getById(id);
        if(exist == null){
            throw new NotFoundException(10002);
        }
        BeanUtils.copyProperties(dto, exist);
        this.celebrityAddressService.updateById(exist);
    }

    public void deleteCelebrityAddress(Long id){
        CelebrityAddressDO exist = this.celebrityAddressService.getById(id);
        if(exist == null){
            throw new NotFoundException(10002);
        }
        this.celebrityAddressService.getBaseMapper().deleteById(id);
    }
}
